package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.User;
import com.example.demo.service.AuthenticationService;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@Api(value = "登录验证接口", tags = "登陆验证接口" )
public class AuthenticationController {
    private AuthenticationService authenticationService;
    private UserService userService;

    @Autowired
    public AuthenticationController(AuthenticationService authenticationService, UserService userService) {
        this.authenticationService = authenticationService;
        this.userService = userService;
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    @ApiOperation(value = "", notes = "显示未开始需求")
    public Object login(@RequestBody User user) {
        User userInDataBase = userService.findByName(user.getUsername());
        JSONObject jsonObject = new JSONObject();
        if (userInDataBase == null) {
            jsonObject.put("result", "0");
        } else if (!userService.comparePassword(user, userInDataBase)) {
            jsonObject.put("result", "1");
        } else {
//            String token = authenticationService.getToken(userInDataBase);
//            jsonObject.put("token", token);
            jsonObject.put("result","2");
            jsonObject.put("username", userInDataBase.getUsername());
            jsonObject.put("admin", userInDataBase.getAdmin());
        }
        return jsonObject;
    }
}
